package kr.saeildamil.androidstudy.ch12;

import java.util.ArrayList;

import kr.saeildamil.androidstudy.R;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class ListAddDelEx extends Activity {
	ArrayList<String> arList;
	ArrayAdapter<String> arrayAdapter;
	ListView list;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_listadddel);

		arList = new ArrayList<String>();
		arList.add("First");
		arList.add("Second");
		arList.add("Third");

		arrayAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_single_choice, arList);

		list = (ListView) findViewById(R.id.ch12_listadddel_list);
		list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		list.setAdapter(arrayAdapter);
		list.setDivider(new ColorDrawable(Color.BLUE));
		list.setDividerHeight(3);
		list.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(ListAddDelEx.this, arList.get(position),
						Toast.LENGTH_LONG).show();

			}
		});
	}

	public void mOnClick(View v) {
		EditText et = (EditText) findViewById(R.id.ch12_listadddel_edit);
		switch (v.getId()) {
		case R.id.ch12_listadddel_add:
			String str = et.getText().toString();
			arList.add(str);
			arrayAdapter.notifyDataSetChanged();
			et.setText("");
			break;

		case R.id.ch12_listadddel_del:
			int pos = list.getCheckedItemPosition();
			if (pos != ListView.INVALID_POSITION) {
				arList.remove(pos);
				list.clearChoices();
				arrayAdapter.notifyDataSetChanged();
			}
			break;
		}
	}
}
